//
//  SearchView.m
//  ljhoo_ios
//
//  Created by abc123 on 15/8/30.
//  Copyright (c) 2015年 ljhoo. All rights reserved.
//

#import "SearchView.h"
#import "Size.h"
@interface SearchView () {
    UIView *parentView;
    ResponseCallback callback;

}

@end

@implementation SearchView

- (id)initWithParent:(UIView*)_parentView _point:(CGPoint)_point  _placeholder:(NSString*)_placeholder _callback:(ResponseCallback)_callback {
    self = [super init];
    parentView = _parentView;
    callback = _callback;

    self.frame = CGRectMake(_point.x, _point.y, _parentView.frame.size.width - _point.x,50);
    self.backgroundColor = GRAYCOLOR;
    [_parentView addSubview:self];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 0.5, WindowWidth/3, 0.5)];
    line.backgroundColor = [UIColor grayColor];
//    [self addSubview:line];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 15, 40, 30)];
    [button addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button setBackgroundColor:[UIColor blueColor]];
    [self addSubview:button];
    float txt_h = self.frame.size.height - 10 * 2;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(button.frame), button.frame.origin.y, 5, txt_h)];
    label.backgroundColor = [UIColor whiteColor];
    label.layer.borderColor = [UIColor blueColor].CGColor;
    label.layer.borderWidth = 1;
    
//    [self addSubview:label];
//    float txt_x = CGRectGetMaxX(label.frame);
    self.txtSearch = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(button.frame), 15, WindowWidth/3*1.5, txt_h)];
    _txtSearch.backgroundColor = [UIColor whiteColor];
    _txtSearch.placeholder = _placeholder;
    _txtSearch.layer.borderWidth = 1;
    _txtSearch.delegate = self;
    _txtSearch.layer.borderColor = [UIColor blueColor].CGColor;
    _txtSearch.font = FONT(14);
    [self addSubview:_txtSearch];
    
    
    
    float btn_size = _txtSearch.frame.size.height * 1.2;
    
    
    float img_h = btn_size * 0.49, img_w = img_h * 1.076, img_x = (btn_size - img_w) / 2, img_y = (btn_size - img_h) / 2;
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(img_x, img_y - 2, img_w, img_h)];
    img.image = [UIImage imageNamed:@"search.png"];
    [button addSubview:img];
    return self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)doClick:(UIButton*)btn {
    [parentView endEditing:YES];
    if (nil != callback) {
        callback([NSMutableDictionary dictionaryWithDictionary:@{@"keyword" : _txtSearch.text}]);
    }
}

@end
